/***************************************************************************
   Filename:    MainMenu.cpp
   Author:      Josh Partridge
   Description: Contains the MainMenu class, basically for switching states
****************************************************************************/

#include "MainMenu.h"
#include <iostream>

MainMenu::MainMenu()
{
    paused = false;
    menuState = NEW_GAME;
}

/************************************************************************************
 Name:               unlockContine
 Description:        sets paused to true (so CONTINUE option is available)
*************************************************************************************/
void MainMenu::unlockContinue()
{
    paused = true;
}

/************************************************************************************
 Name:               lockContinue
 Description:        sets paused to false (so CONTINUE option is unavailable)
*************************************************************************************/
void MainMenu::lockContinue()
{
    paused = false;
    menuState = NEW_GAME;
}

/************************************************************************************
 Name:               run
 Description:        contains the loop that keeps us in the main menu
 Input:
       stateMachine: reference to main's StateMachine
*************************************************************************************/
void MainMenu::run(StateMachine& stateMachine, Brush& brush)
{
    char ch;
    chosen = false;

    brush.clear();

    //draw a border
    brush.setColor(blue);
    doubleLineBox(brush, 3, 2, 113, 35);

    //reset the color to normal
    brush.setColor(white);

    brush.clearInput();

    // menu loop
    while(!chosen)
    {

        dispOptions(brush);

        ch = getInput();

        handleInput(ch);

    }

    stateMachine.setState(menuState);

    if(paused)
    {
        menuState = CONTINUE;
    }
    else
    {
        menuState = NEW_GAME;
    }
}

/************************************************************************************
 Name:               dispOptions
 Description:        prints the options and highlights the current choice
 Input:
              brush: reference to main's brush object
*************************************************************************************/
void MainMenu::dispOptions(Brush &brush)
{
    int offset = 0;

    if(paused)
    {
        offset = 2;
        brush.move(56, 7+offset);
        cout << " CONTINUE ";
    }

    brush.move(56, 12+offset);
    cout << " NEW GAME ";

    brush.move(54, 17+offset);
    cout << " INSTRUCTIONS ";

    brush.move(55, 22+offset);
    cout << " SCOREBOARD ";

    brush.move(58, 27+offset);
    cout << " QUIT ";

    brush.setColor(cyan);

    switch (menuState)
    {
        case CONTINUE:     brush.move(56,7+offset);
                           cout << "-CONTINUE-";
                           break;

        case NEW_GAME:     brush.move(56,12+offset);
                           cout << "-NEW GAME-";
                           break;

        case INSTRUCTIONS: brush.move(54,17+offset);
                           cout << "-INSTRUCTIONS-";
                           break;

        case SCOREBOARD:   brush.move(55,22+offset);
                           cout << "-SCOREBOARD-";
                           break;

        case QUIT:         brush.move(58,27+offset);
                           cout << "-QUIT-";
        default:;
    }

    brush.setColor(white);
}

/************************************************************************************
 Name:               handleInput
 Description:        calls cycleState or sets choosen to true when appropriate
 Input:
                 ch: The key the user has pressed ( provided by getInput() in helpers)
*************************************************************************************/
void MainMenu::handleInput(char ch)
{
    switch(ch)
    {
        case AR_UP: cycleState("up"); break;
        case AR_DOWN: cycleState("down"); break;
        case 'x':
        case ESC: cycleState("esc"); break;
        case 'z':
        case ' ':
        case ENTER: chosen = true;
        default:;
    }
}

/************************************************************************************
 Name:               cycleState
 Description:        increment or decrement the current option
 Input:
                key: The key the user has pressed ("up", "down", or "esc") (string)
*************************************************************************************/
void MainMenu::cycleState(string key)
{
    switch (menuState)
    {
        case CONTINUE:
            if(key == "up")
                menuState = QUIT;
            else if (key == "down")
                menuState = NEW_GAME;
            else if (key == "esc")
                menuState = QUIT;
            break;
        case NEW_GAME:
            if(key == "up" && paused)
                menuState = CONTINUE;
            else if (key == "up")
                menuState = QUIT;
            else if (key == "down")
                menuState = INSTRUCTIONS;
            else if (key == "esc")
                menuState = QUIT;
            break;
        case INSTRUCTIONS:
            if(key == "up")
                menuState = NEW_GAME;
            else if (key == "down")
                menuState = SCOREBOARD;
            else if (key == "esc")
                menuState = QUIT;
            break;
        case SCOREBOARD:
            if(key == "up")
                menuState = INSTRUCTIONS;
            else if (key == "down")
                menuState = QUIT;
            else if (key == "esc")
                menuState = QUIT;
            break;
        case QUIT:
            if(key == "down" && paused)
                menuState = CONTINUE;
            else if (key == "up")
                menuState = SCOREBOARD;
            else if (key == "down")
                menuState = NEW_GAME;
            else if (key == "esc")
                chosen = true;
            break;
        default:;
    }
}
